#include "contractor-delegate.h"
#include "date-time-edit-delegate.h"
#include "debug-helper.h"
#include "double-spin-box-delegate.h"
#include "order-section-model.h"
#include "order-section-queries.h"
#include "qxtsignalgroup.h"
#include "value-result.h"
#include "worker-delegate.h"

OrderSectionModel::OrderSectionModel(QObject *parent) :
    StandardDatabaseModel(parent), m_workerDelegate(0), m_contractorDelegate(0)
{
}

void OrderSectionModel::initialize()
{
    m_queries = new OrderSectionQueries(this);
    m_workerDelegate = new WorkerDelegate(this);
    m_workerDelegate->setDatabaseClient(m_databaseClient);
    m_contractorDelegate = new ContractorDelegate(this);
    m_contractorDelegate->setDatabaseClient(m_databaseClient);
    DateTimeEditDelegate* dateDelegate = new DateTimeEditDelegate(this);
    DoubleSpinBoxDelegate* sumDelegate = new DoubleSpinBoxDelegate(this);
    m_delegates.insert(WorkerColumn, m_workerDelegate);
    m_delegates.insert(ContractorColumn, m_contractorDelegate);
    m_delegates.insert(DateColumn, dateDelegate);
    m_delegates.insert(SumColumn, sumDelegate);
    m_headerSections << "Pracownik" << "Kontrahent" << "Data" << "Suma";
    m_dataAlreadyExistText = "Zamówienie o takich danych już istnieje.";

    QxtSignalGroup* m_delegateSignalGroup = new QxtSignalGroup(this);
    m_delegateSignalGroup->addSignal(m_workerDelegate, SIGNAL(initialized()));
    m_delegateSignalGroup->addSignal(m_contractorDelegate, SIGNAL(initialized()));
    connect (m_delegateSignalGroup, &QxtSignalGroup::allSignalsReceived, this, &OrderSectionModel::finishInitialize);
    connect (m_delegateSignalGroup, SIGNAL(allSignalsReceived()), m_delegateSignalGroup, SLOT(deleteLater()));
    m_workerDelegate->initialize();
    m_contractorDelegate->initialize();
}

void OrderSectionModel::finishInitialize()
{
    StandardDatabaseModel::initialize();
}

ValueResult* OrderSectionModel::canSetField (QVariant value, int column, int row)
{
    Q_UNUSED(value);
    Q_UNUSED(column);
    Q_UNUSED(row);
    ValueResult* result = new ValueResult(this);
    result->setValue(true);
    return result;
}

void OrderSectionModel::setupInternalModel()
{
    StandardDatabaseModel::setupInternalModel();
    const QList<QPair<QString,QVariant>>& workerData = m_workerDelegate->comboBoxData();
    const QList<QPair<QString,QVariant>>& contractorData = m_contractorDelegate->comboBoxData();

    for (int i = 0; i < rowCount(); i++)
    {
        // worker column
        QVariant data = m_internalModel->index(i, WorkerColumn+m_idColumnCount).data();
        int j;
        for (j = 0; j < workerData.size(); j++)
        {
            if (workerData.at(j).second == data)
            {
                m_internalModel->setData(m_internalModel->index(i, WorkerColumn+m_idColumnCount), workerData.at(j).first, Qt::DisplayRole);
                break;
            }
        }
        if (j == workerData.size())
        {
            fatalError(data.toInt());
            return;
        }

        // contractor column
        data = m_internalModel->index(i, ContractorColumn+m_idColumnCount).data();
        for (j = 0; j < contractorData.size(); j++)
        {
            if (contractorData.at(j).second == data)
            {
                m_internalModel->setData(m_internalModel->index(i, ContractorColumn+m_idColumnCount), contractorData.at(j).first, Qt::DisplayRole);
                break;
            }
        }
        if (j == contractorData.size())
        {
            fatalError(data.toInt());
            return;
        }

        // date column
        QDateTime date = QDateTime::fromString(m_internalModel->index(i, DateColumn+m_idColumnCount).data().toString(), Qt::ISODate);
        m_internalModel->setData(m_internalModel->index(i, DateColumn+m_idColumnCount), date.toString("yyyy.MM.dd h:mm:ss"), Qt::DisplayRole);

        // sum column
        QString sum = QLocale().toString(m_internalModel->index(i, SumColumn+m_idColumnCount).data().toDouble(), 'f', 2);
        m_internalModel->setData(m_internalModel->index(i, SumColumn+m_idColumnCount), sum, Qt::DisplayRole);
    }
}
